﻿using System.Collections.Generic;
using Dolany.UtilityTool;
using Missbot.Core.Base;

namespace Missbot.Core.Cache
{
    /// <summary>
    /// 发送缓存
    /// </summary>
    public class OutputBuffer
    {
        /// <summary>
        /// 信息原始Session
        /// </summary>
        private InboxSession Session { get; }

        /// <summary>
        /// 附件ID
        /// </summary>
        private string FileID { get; set; }

        /// <summary>
        /// 分段消息列表
        /// </summary>
        private List<string> MsgList { get; } = new();

        /// <summary>
        /// 用信息原始Session初始化发送缓存
        /// </summary>
        /// <param name="session"></param>
        public OutputBuffer(InboxSession session)
        {
            this.Session = session;
        }

        /// <summary>
        /// 添加消息缓存
        /// </summary>
        /// <param name="content"></param>
        public void Add(string content)
        {
            MsgList.Add(content);
        }

        /// <summary>
        /// 添加信息对
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddPair(string key, string value)
        {
            Add($"**{key}**: {value}");
        }

        /// <summary>
        /// 按字典依次添加信息对
        /// </summary>
        /// <param name="pairDic"></param>
        public void AddPair(Dictionary<string, string> pairDic)
        {
            foreach (var (key, value) in pairDic)
            {
                AddPair(key, value);
            }
        }

        /// <summary>
        /// 添加消息附件
        /// </summary>
        /// <param name="fileID"></param>
        public void AddFile(string fileID)
        {
            FileID = fileID;
        }

        /// <summary>
        /// 添加多个消息缓存
        /// </summary>
        /// <param name="contents"></param>
        public void Add(IEnumerable<string> contents)
        {
            MsgList.AddRange(contents);
        }

        /// <summary>
        /// 将缓存清空并发送
        /// </summary>
        public void Send()
        {
            var content = MsgList.JoinToString("\n");
            Session.Channel.Send(Session, content, FileID);
        }
    }
}